function amrun
%AMRUN Summary of this function goes here
%   Detailed explanation goes here


%% setup
global amscan;
global amaux;
global amrack;


%% record constants
%maybe create const class?
%amscan.consts.data=amget([amscan.consts.rackchan]); % what is this going to look like?
%amscan.consts.names=[amscan.consts.rackchan.name];
%% initialize structures
dataindex={};
nloops=length(amscan.loops);
for i=1:nloops
    dataindex{end+1}=amscan.loops(i).npoints;
    for j=1:length(amscan.loops(i).setchans) 
        amscan.data.(amscan.loops(i).setchans(j).name)=nan(dataindex{:});
    end

    for j=1:length(amscan.loops(i).getchans) 
        amscan.data.(amscan.loops(i).getchans(j).name)=nan(dataindex{:});
    end
end

%% setup plots
fighand=figure;
plots=[amscan.loops.plots];
nplots=length(plots);
switch nplots
    case 1
        prow=1;pcol=1;
    case 2
        prow=1;pcol=2;
    case {3,4}
        prow=2;pcol=2;
    case {5,6}
        prow=2;pcol=3;
    case {7,8,9}
        prow=3;pcol=3;
    otherwise
        error('Too many plots');
end
for i=1:nplots
    plots(i).axishands=subplot(prow,pcol,i);
    switch plots(i).plottype
        case 'XY'
            if length(plots(i).ychan)==1
                plots(i).objhands=plot(plots(i).axishands,0,0);
            elseif length(plots(i).ychan)==2
                [plots(i).axishands plots(i).objhands(1) plots(i).objhands(2)]=plotyy(plots(i).axishands,0,0,0,0);
            end
        case 'XYZ'
            plots(i).objhands=pcolor(plots(i).axishands,0,0,0);
    end
end

%% run channels
amrunloop(length(amscan.loops),{});
%% save and log data